from PySide2.QtWidgets import QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PySide2.QtCore import Signal

class PageNumber(QWidget):
    
    control_signal = Signal(int)
    page = 10

    def __init__(self):
        super(PageNumber, self).__init__()

        self.setupUi()

    def setPage(self, number):
        self.page = number
        self.totalPage.setText("共" + str(self.page) + "页")
        self.curPage.setText("1")
        self.skipPage.setText("")

    def reset(self):
        self.curPage.setText("1")
        self.skipPage.setText("")

    def setupUi(self):
        control_layout = QHBoxLayout(self)
        homePage = QPushButton("首页")
        prePage = QPushButton("<")
        self.curPage = QLabel("1")
        nextPage = QPushButton(">")
        finalPage = QPushButton("尾页")
        self.totalPage = QLabel("共" + str(self.page) + "页")
        skipLable_0 = QLabel("跳到")
        self.skipPage = QLineEdit()
        skipLabel_1 = QLabel("页")
        confirmSkip = QPushButton("确定")
        homePage.clicked.connect(self.__home_page)
        prePage.clicked.connect(self.__pre_page)
        nextPage.clicked.connect(self.__next_page)
        finalPage.clicked.connect(self.__final_page)
        confirmSkip.clicked.connect(self.__confirm_skip)
        control_layout.addStretch(1)
        control_layout.addWidget(homePage)
        control_layout.addWidget(prePage)
        control_layout.addWidget(self.curPage)
        control_layout.addWidget(nextPage)
        control_layout.addWidget(finalPage)
        control_layout.addWidget(self.totalPage)
        control_layout.addWidget(skipLable_0)
        control_layout.addWidget(self.skipPage)
        control_layout.addWidget(skipLabel_1)
        control_layout.addWidget(confirmSkip)
        control_layout.addStretch(1)

    def __home_page(self):
        """点击首页信号"""
        number = int(self.curPage.text())
        if number == 1:
            return
        self.curPage.setText("1")
        self.__control()

    def __pre_page(self):
        """点击上一页信号"""
        number = int(self.curPage.text())
        if number <= 1:
            return
        number -= 1
        self.curPage.setText(str(number))
        self.__control()

    def __next_page(self):
        """点击下一页信号"""
        number = int(self.curPage.text())
        if number >= self.page:
            return
        number += 1
        self.curPage.setText(str(number))
        self.__control()

    def __final_page(self):
        """尾页点击信号"""
        number = int(self.curPage.text())
        if number == self.page:
            return
        self.curPage.setText(str(self.page))
        self.__control()

    def __confirm_skip(self):
        """跳转页码确定"""
        skip = int(self.skipPage.text())
        number = int(self.curPage.text())
        if skip == number:
            return
        if skip > 0 and skip <= self.page:
            self.curPage.setText(str(skip))
            self.skipPage.setText("")
            self.__control()

    def __control(self):
        self.control_signal.emit(int(self.curPage.text()))

    def showTotalPage(self):
        """返回当前总页数"""
        return int(self.totalPage.text()[1:-1])
